package org.dev.app.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.dev.app.service.DeptHystrixService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author wangzhuangzhuang
 * @Description:
 * @date 2022-05-26 16:52
 */
@Slf4j
@RestController
public class HystrixController_Consumer {

	@Resource
	private DeptHystrixService deptHystrixService;

	@RequestMapping(value = "/consumer/dept/hystrix/ok/{id}")
	public String deptInfo_Ok(@PathVariable("id") Integer id) {
		return deptHystrixService.deptInfo_Ok(id);
	}

	// 在客户端进行降级
	@RequestMapping(value = "/consumer/dept/hystrix/timeout/{id}")
	@HystrixCommand(fallbackMethod = "dept_TimeoutHandler") // 为该请求指专属的回退方法
	public String deptInfo_Timeout(@PathVariable("id") Integer id) {
		String s = deptHystrixService.deptInfo_Timeout(id);
		log.info(s);
		return s;
	}

	// deptInfo_Timeout方法的专用 fallback方法
	public String dept_TimeoutHandler(@PathVariable("id") Integer id) {
		log.info("deptInfo_Timeout出错，服务已被降级！");
		return "服务端系统繁忙，请稍后再试！（客户端 deptInfo_Timeout 专属的回退方法触发）";
	}

}

//http://eureka8001.com:8091/dept/hystrix/timeout/1
//		线程池：hystrix-DeptServiceImpl-1 deptInfo_Timeout,id: 1 耗时: 4 , 端口号： 8091
//
//
//http://eureka8001.com:8088/consumer/dept/hystrix/timeout/1
//		服务端系统繁忙，请稍后再试！（客户端 deptInfo_Timeout 专属的回退方法触发）
